// pages/goods/index.js
var http = require("../../utils/http.js")

import api from "../../utils/api.js";
import market_cart from '../../utils/market/market_cart'
import message from "../../utils/message.js";



var getProductNums = function(product_id){
  return 5
}

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    categoryList: [
    ],
    active_index:3,
    productList:[],
    category_product_list:[],
    static_url:'',
    cart_goods_list:[],
    total_price:0,
    is_show_cart_goods:false,
    type:'shopping',
    company_info:null,
    coupou_list:[],
    market_setting:null,
    products_bak:[],  //缓存商品表数据
    product_list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    wx.setTabBarBadge({
      index: 2,
      text: '3',
    })

    this.setData({
      static_url:app.globalData.static_url
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var me = this;
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var me = this;


    /*
    api.product.getCategoryProductList( {} , function (response){
      me.setData({
        category_product_list:response.data,
        products_bak:response.data
      })

      me.getCartProductList();

    })

    */

  
    this.getCategoryList();
    //this.getMerchantInfo();

    this.getCartProductList();

    this.getMarketSetting();
    

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selectCategory:function( e ){
    var me = this;
    var id = e.currentTarget.dataset.id
    this.setData({
      active_index:id
    })


    var params = {};
    params.category_id = id;
    message.show_loading();
    api.product.getProductListByCategory( params ,function (response) {
      message.hide_loading();

      if(response.status==1){
        me.setData({
          product_list: response.data 
        })
      }else{

      }
      
    });



  },
  getCategoryList:function(){
    var me = this;
    message.show_loading();
    api.product.getCategoryList({} , function (response) {
      message.hide_loading();

      me.setData({
        product_list:  response.data.product_list,
        category_list: response.data.category_list
      })
      
    });
  },
  showDetail:function( e ){
    console.log("加载详情")
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/goods/detail?id='+id,
      success:function(){
      }
    })
  },
  doAddGood:function(e){
    var me = this;

    var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index;

    market_cart.addProductToCart( {product_id: id} , function(response){

      if(response.status==1){
        message.toast(response.message);
        me.setData({
          cart_goods_list:response.data.good_list,
          total_price:response.data.total_price
        })

        me.updateCartProductList( response.data.good_list );
      }
    });
  },
  doReduceGood:function(e){
    var me = this;

    var id = e.currentTarget.dataset.id

    market_cart.reduceProductToCart( {product_id:id} , function(response){

      if(response.status==1){
        message.toast(response.message);
        me.setData({
          cart_goods_list:response.data.good_list,
          total_price:response.data.total_price
        })

        me.updateCartProductList( response.data.good_list );
      }
    });


  },
  showCartGoods:function(e){
    var action = e.currentTarget.dataset.action;
    if(action==1){
      this.setData({
        is_show_cart_goods:true
      })
    }

    if(action==-1){
      this.setData({
        is_show_cart_goods:false
      })
    }
  },
  clearCartGoods:function(){
    var me = this;

    
    market_cart.clearCartGoods({}  , function(response){
      
      if(response.status==1){
        me.setData({
          cart_goods_list:[],
          total_price:0
        })
      }else{
        message.show_error(response.message)
      }
    })

  },
  doCreateOrder:function(){
    wx.navigateTo({
      url: '/pages/market/market-order-create'
    })
  },
  selectType:function( e ){
    var type= e.currentTarget.dataset.type;

    this.setData({
      type:type
    })
    
  },
  getCartProductList:function(){
    var me = this;
    message.show_loading();
    market_cart.getCartGoodList({} , function( response ){
      message.hide_loading();
      if(response.status==1){
        me.setData({
          cart_goods_list: response.data.good_list ,
          total_price:response.data.total_price
        })
        setTimeout(function(){
          me.updateCartProductList( response.data.good_list )
        },500)
       
      }else{

      }
    })
  },
  updateCartProductList:function(  cart_goods_list  ){
    //将购物车数据与当前展示商品的购物数量重新比对


    var data = this.data.products_bak;
    var new_data = JSON.parse(JSON.stringify(data)) ;//重新生成一个商品备份数据

    var product_list = this.data.product_list;

    if(cart_goods_list.length>0){

      for(var i=0;i<cart_goods_list.length; i++){
        for(var j=0;j<product_list.length ; j++){
          if( product_list[j].id==cart_goods_list[i].product_id){
            product_list[j].nums = cart_goods_list[i].product_nums;
            product_list[j].sp=1;
          }else{
            if(!product_list[j].sp){
              product_list[j].nums=0;
            }
            
          }
        }
      }
    }else{

      for(var j=0;j<product_list.length ; j++){
        product_list[j].nums=0;
      }


    }


    /*
    for(var i=0;i<cart_goods_list.length; i++){

      for(var t = 0;t<new_data.length; t++){
        var products = new_data[t].products;
        for(var j=0;j<products.length ; j++){
          

          if( products[j].id==cart_goods_list[i].product_id){
            console.log(i+"__"+t+"__"+j)
            products[j].nums = cart_goods_list[i].product_nums;
            products[j].sp=1;
          }else{
            if(!products[j].sp){
              products[j].nums=0;
            }
            
          }
        }
        
      }
    }

    */
    this.setData({
      product_list: product_list
    })
  },
  getMarketSetting:function(){
    var me = this;
    market_cart.getMarketSetting({} , function(response){

      if(response.status==1){

        me.setData({
          market_setting:response.data
        })
      }
    });
  }

})